//home模块的小仓库

// 引入API接口函数回调
import { reqCategoryList, reqGetBannerList, reqGetFloorList } from '@/api/index'

//state:仓库存储数据的地方
const state = {
    //state默认初始数据不能瞎写,根据接口返回值去初始化的,
    // 三级菜单
    categoryList: [],
    bannerList: [],
    floorList: []

};
//mutations :修改state的唯一手段
const mutations = {
    //三级菜单
    CATEGORYLIST(state, categoryList) {
        state.categoryList = categoryList.slice(0, 16)
    },
    // 修改轮播图 banner 数据
    GETBANNERLIST(state, bannerList) {
        state.bannerList = bannerList
    },
    //修改floor数据
    GETFLOORLIST(state, floorList) {
        state.floorList = floorList
    }

};
//actions:处理业务逻辑，也可以异步任务
const actions = {
    //通过API里面的接口函数调用，向服务器发送请求，获取服务器的数据
    // async categoryList(context) {
    //     let result = await reqCategoryList();
    //     if (result.code === 200) {
    //         context.commit('CATEGORYLIST', result.data)
    //     }
    // }
    // 结构赋值写法  三级菜单
    async categoryList({ commit }) {
        try {
            let result = await reqCategoryList();
            if (result.code == 200) {

                commit('CATEGORYLIST', result.data)
            }
        } catch (e) {
            console.log(e);
        }

    },
    //获取 轮播图banner  数据
    async getBannerList({ commit }) {
        let result = await reqGetBannerList();
        if (result.code == 200) {
            commit('GETBANNERLIST', result.data)
        }
    },
    // 获取floor数据
    async getFloorList({ commit }) {
        let result = await reqGetFloorList()
        if (result.code == 200) {
            commit('GETFLOORLIST', result.data)
        }
    }
};
//getters:理解为计算属性，用于简化仓库数据，让组件获取仓库数据更加方便
const getters = {}

export default {
    state,
    mutations,
    actions,
    getters
}